--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    doer:PushEvent("hutao_event.stewer_cooking.start",{
        product = self.product,
        stewer = self,
        items = items,          -- {item_prefab1, item_prefab2, item_prefab3, item_prefab4}
    })

    self.__hutao_cooker:PushEvent("hutao_event.stewer_cooking.done",{
        product = self.product,
        stewer = self,
        items = self.__hutao_items,
    })

    doer:PushEvent("hutao_event.stewer_cooking.pre_harvest",{
        product = self.product,
        stewer = self,
    })


    怪物度大于1时：33%怪物千层糕，33%怪物鞑靼，32%正常判定，2%梅菜扣肉*
    否则，肉度不小于2时：50%正常判定，15%怪物千层糕，10%怪物鞑靼，9%鳄梨酱，9%骨头汤，7%梅菜扣肉*。
    否则，有鱼度时：50%正常判定，10%素鲍鱼，15%蛙腿三明治，15%酿鱼头，5%蓝带鱼排，5%幽幽大行军*。
    否则，不为失败作时：50%正常判定，15%爆炒填馅辣椒，15%辣龙椒沙拉，15%恐怖国王饼，5%梅花酿*。
    失败作配方就只能做出失败作
    用梅花进行烹饪动作时，会有台词：
    本堂主今天可要好好地露一手~/往生堂八大菜系之梅花菜，瞧好咯！
    不用梅花进行烹饪动作时，会有台词：

]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--
    local function IsTalentUnlocked(inst)
        if inst.components.hutao_com_talent_tree and 
            inst.components.hutao_com_talent_tree:IsUnlocked("the_more_the_better") then 
            return true
        end
        return false
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 烹饪 API
    local cooking = require("cooking")
    local  ingredients = cooking.ingredients
    local GetRecipe = cooking.GetRecipe

    --our naming conventions aren't completely consistent, sadly
    local aliases =
    {
        cookedsmallmeat = "smallmeat_cooked",
        cookedmonstermeat = "monstermeat_cooked",
        cookedmeat = "meat_cooked",
    }
    local function GetIngredientValues(prefablist)
        local prefabs = {}
        local tags = {}
        for k,v in pairs(prefablist) do
            local name = aliases[v] or v
            prefabs[name] = (prefabs[name] or 0) + 1
            local data = ingredients[name]
            if data ~= nil then
                for kk, vv in pairs(data.tags) do
                    tags[kk] = (tags[kk] or 0) + vv
                end
            end
        end
        return { tags = tags, names = prefabs }
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 烹饪覆盖
    --[[
        Braised Pork Belly with Preserved Vegetable -- 梅菜扣肉
        Vegetarian Abalone  -- 素鲍鱼
        Ghostly March -- 幽幽大行军
        Plum Blossom Wine   -- 梅花酿

        台词index
            cooking.braised_pork_belly_with_preserved_vegetable -- 梅菜扣肉
            cooking.vegetarian_abalone -- 素鲍鱼
            cooking.ghostly_march -- 幽幽大行军
            cooking.plum_blossom_wine -- 梅花酿
            cooking.failed            -- 失败作
            cooking.wrong_product        -- 错误作
            cooking.start_with_plum_blossom -- 用梅花进行烹饪动作时
            cooking.start_without_plum_blossom -- 不用梅花进行烹饪动作时


    ]]--
    local BRAISED_PORK_BELLY_WITH_PRESERVED_VEGETABLE = "hutao_cooked_food_braised_pork_belly_with_preserved_vegetable" -- 梅菜扣肉
    local VEGETARIAN_ABALONE = "hutao_cooked_food_vegetarian_abalone" -- 素鲍鱼
    local GHOSTLY_MARCH = "hutao_cooked_food_ghostly_march" -- 幽幽大行军
    local PLUM_BLOSSOM_WINE = "hutao_cooked_food_plum_blossom_wine" -- 梅花酿
    local COOK_FAILED_PRODUCT = "wetgoop"   -- 失败作


    local function GetProductReplace(inst,IngredientValues,origin_product)
        if (IngredientValues.tags.monster or 0) > 1 then
                    -- 怪物度大于1时：33%怪物千层糕，33%怪物鞑靼，32%正常判定，2%梅菜扣肉*
                    if math.random() < 0.33 then
                        return "monsterlasagna",origin_product~="monsterlasagna" and "cooking.wrong_product"    -- 怪物千层饼
                    elseif math.random() < 0.66 then
                        return "monstertartare",origin_product~="monstertartare" and "cooking.wrong_product"    -- 怪物鞑靼
                    elseif math.random() < 0.98 then
                        return origin_product
                    else
                        if IsTalentUnlocked(inst) then
                            return BRAISED_PORK_BELLY_WITH_PRESERVED_VEGETABLE ,"cooking.braised_pork_belly_with_preserved_vegetable" -- 梅菜扣肉
                        else
                            return COOK_FAILED_PRODUCT , "cooking.failed" -- 失败作
                        end
                    end

        elseif (IngredientValues.tags.meat or 0) >= 2 then
                    -- 肉度不小于2时：50%正常判定，15%怪物千层糕，10%怪物鞑靼，9%鳄梨酱，9%骨头汤，7%梅菜扣肉*。
                    if math.random() < 0.5 then
                        return origin_product
                    elseif math.random() < 0.65 then
                        return "monsterlasagna",origin_product~="monsterlasagna" and "cooking.wrong_product"    -- 怪物千层饼
                    elseif math.random() < 0.75 then
                        return "monstertartare",origin_product~="monstertartare" and "cooking.wrong_product"    -- 怪物鞑靼
                    elseif math.random() < 0.84 then
                        return "guacamole",origin_product~="guacamole" and "cooking.wrong_product"    -- 鳄梨酱
                    elseif math.random() < 0.93 then
                        return "bonestew",origin_product~="bonestew" and "cooking.wrong_product"    -- 骨头汤
                    else
                        if IsTalentUnlocked(inst) then
                            return BRAISED_PORK_BELLY_WITH_PRESERVED_VEGETABLE ,"cooking.braised_pork_belly_with_preserved_vegetable" -- 梅菜扣肉
                        else
                            return COOK_FAILED_PRODUCT , "cooking.failed" -- 失败作
                        end
                    end

        elseif (IngredientValues.tags.fish or 0) >= 0 then
                    -- 有鱼度时：50%正常判定，10%素鲍鱼，15%蛙腿三明治，15%酿鱼头，5%蓝带鱼排，5%幽幽大行军*。
                    if math.random() < 0.5 then
                        return origin_product
                    elseif math.random() < 0.6 then
                        return VEGETARIAN_ABALONE,origin_product~=VEGETARIAN_ABALONE and "cooking.wrong_product"    -- 素鲍鱼
                    elseif math.random() < 0.75 then
                        return "frogglebunwich",origin_product~="frogglebunwich" and "cooking.wrong_product"    -- 蛙腿三明治
                    elseif math.random() < 0.9 then
                        return "barnaclestuffedfishhead",origin_product~="barnaclestuffedfishhead" and "cooking.wrong_product"    -- 酿鱼头
                    elseif math.random() < 0.95 then
                        return "frogfishbowl",origin_product~="frogfishbowl" and "cooking.wrong_product"    -- 蓝带鱼排
                    else
                        if IsTalentUnlocked(inst) then
                            return GHOSTLY_MARCH ,"cooking.ghostly_march" -- 幽幽大行军
                        else
                            return COOK_FAILED_PRODUCT , "cooking.failed" -- 失败作
                        end
                    end
        elseif origin_product ~= "wetgoop" then                    
                    -- 不为失败作时：50%正常判定，15%爆炒填馅辣椒，15%辣龙椒沙拉，15%恐怖国王饼，5%梅花酿*。
                    if math.random() < 0.5 then
                        return origin_product
                    elseif math.random() < 0.65 then
                        return "pepperpopper",origin_product~="pepperpopper" and "cooking.wrong_product"    -- 爆炒填馅辣椒
                    elseif math.random() < 0.8 then
                        return "dragonchilisalad",origin_product~="dragonchilisalad" and "cooking.wrong_product"    -- 辣龙椒沙拉
                    elseif math.random() < 0.95 then
                        return "nightmarepie",origin_product~="nightmarepie" and "cooking.wrong_product"    -- 恐怖国王饼
                    else
                        if IsTalentUnlocked(inst) then
                            return PLUM_BLOSSOM_WINE ,"cooking.plum_blossom_wine" -- 梅花酿
                        else
                            return COOK_FAILED_PRODUCT , "cooking.failed" -- 失败作
                        end
                    end


        end
        return origin_product
    end

    ---- 替换烹饪数量
    local function Get_Num_To_Give(product)
        local cookpots = {"cookpot","portablecookpot","archive_cookpot"}
        for _,cookpot in ipairs(cookpots) do
            local temp_recipe = GetRecipe(cookpot,product)
            if type(temp_recipe) == "table" and temp_recipe.stacksize then
                return temp_recipe.stacksize
            end
        end
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 采集的瞬间 替换。
    local function GetHarvestReplace(inst,origin_product)
        -- if origin_product == "barnaclestuffedfishhead" then
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 烹饪开始检查
    local function IsCookingWithPlumBlossom(IngredientValues)
        return false
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    if not TheWorld.ismastersim then
        return
    end
    local done_dialogue_indexs = {}
    inst:ListenForEvent("hutao_event.stewer_cooking.start",function(inst,_table)
        local stewer = _table and _table.stewer
        local items = _table and _table.items
        local origin_product = _table and _table.product
        if stewer and items and origin_product then
            local IngredientValues = GetIngredientValues(items)
            local done_dialogue_index = nil
            stewer.product,done_dialogue_index = GetProductReplace(inst,IngredientValues,origin_product)
            if done_dialogue_index then
                done_dialogue_indexs[stewer.inst] = done_dialogue_index
            end
            if IsCookingWithPlumBlossom(IngredientValues) then
                inst.components.hutao_com_dialogue:Say("cooking.start_with_plum_blossom")
            elseif math.random() < 0.1 then
                inst.components.hutao_com_dialogue:Say("cooking.start_without_plum_blossom")
            end

        end
    end)
    inst:ListenForEvent("hutao_event.stewer_cooking.done",function(inst,_table)
        local stewer = _table and _table.stewer
        local items = _table and _table.items
        local product = _table and _table.product
        if stewer and done_dialogue_indexs[stewer.inst] then
            inst.components.hutao_com_dialogue:Say(done_dialogue_indexs[stewer.inst])
            done_dialogue_indexs[stewer.inst] = nil
        end
    end)

    inst:ListenForEvent("hutao_event.stewer_cooking.pre_harvest",function(inst,_table)
        local stewer = _table and _table.stewer
        local product = _table and _table.product
        if stewer and product then
            local done_dialogue_index = nil
            local replaced_product = nil
            replaced_product,done_dialogue_index = GetHarvestReplace(inst,product)
            if replaced_product then
                stewer.product = replaced_product
                -- local num_to_give = Get_Num_To_Give(replaced_product)
            end
            if done_dialogue_index then
                inst.components.hutao_com_dialogue:Say(done_dialogue_index)
            end
        end
    end)

end